import Modules from '../config/Modules';
import PathHelper from './PathHelper';

export default class ModulesHelper {
  static buildWebAppExecuteParams = (module: string) => {
    const pathRelativeToAppRoot = module === 'main'
      ? './main'
      : `./subApps/${module}`;
    const commandRunDirectory = PathHelper.getPathRelativeToRoot('../app');
    const webAppDirectory = PathHelper.getPathRelativeToRoot('../app', pathRelativeToAppRoot);
    const viteConfigPath = `${pathRelativeToAppRoot}/vite.config.ts`;
    PathHelper.checkFileExists(commandRunDirectory, viteConfigPath);
    return { commandRunDirectory, webAppDirectory, pathRelativeToAppRoot, viteConfigPath };
  };

  static buildExternalWebAppYarnCommand = (module: string, scriptName: string) => {
    const { nodeVersion } = Modules.getAppConfig(module);
    if (nodeVersion) {
      return `source $NVM_DIR/nvm.sh && nvm use v${nodeVersion} && yarn ${scriptName}`;
    }
    return `yarn ${scriptName}`;
  };

  static buildExternalWebAppExecuteParams = (module: string) => {
    const pathRelativeToAppRoot = `./subApps/${module}`;
    const commandRunDirectory = PathHelper.getPathRelativeToRoot('../app', pathRelativeToAppRoot);
    PathHelper.checkFileExists(commandRunDirectory);
    return { commandRunDirectory, pathRelativeToAppRoot };
  };

  static buildNodeServiceExecuteParams = (module: string) => {
    const pathRelativeToAppRoot = `./${module}Service`;
    const commandRunDirectory = PathHelper.getPathRelativeToRoot('../', pathRelativeToAppRoot);
    PathHelper.checkFileExists(commandRunDirectory);
    return { commandRunDirectory, pathRelativeToAppRoot };
  };
}
